﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utilities;
using ArtOfTest.WebAii.Silverlight.UI;
using ArtOfTest.WebAii.ObjectModel;
using System.Diagnostics;
using ArtOfTest.WebAii.Silverlight;

namespace SM2Input
{
    public class Silverlight
    {
        //Button
        public static void ClickButton(string stringAutomationId)
        {
            //Start count time
            Stopwatch stopWatch = new Stopwatch();
            stopWatch.Start();
            while (true)
            {
                Utilities.GlobalObject.SILVERLIGHTAPP.RefreshVisualTrees();
                Button btn = Utilities.GlobalObject.SILVERLIGHTAPP.FindName<Button>(stringAutomationId);
                try
                {
                    btn.User.Click();
                    System.Threading.Thread.Sleep(Utilities.GlobalConfig.INT_WAIT_INTERVAL_MILISECOND);
                    break;
                }
                catch { }
                Utilities.WaitUtil.checkIfTimeWasOut(stopWatch);
            }
        }

        public static void ClickButton(Button btn)
        {
            //Start count time
            Stopwatch stopWatch = new Stopwatch();
            stopWatch.Start();
            while (true)
            {
                try
                {
                    btn.User.Click();
                    System.Threading.Thread.Sleep(Utilities.GlobalConfig.INT_WAIT_INTERVAL_MILISECOND);
                    break;
                }
                catch { }
                Utilities.WaitUtil.checkIfTimeWasOut(stopWatch);
            }
        }

        //TextField
        public static void TypeTextBox(string stringAutomationId, string stringInputText)
        {
            //Start count time
            Stopwatch stopWatch = new Stopwatch();
            stopWatch.Start();
            while (true)
            {
                Utilities.GlobalObject.SILVERLIGHTAPP.RefreshVisualTrees();
                TextBox tbName = Utilities.GlobalObject.SILVERLIGHTAPP.FindName<TextBox>(stringAutomationId);
                System.Threading.Thread.Sleep(Utilities.GlobalConfig.INT_WAIT_INTERVAL_MILISECOND);
                try
                {
                    tbName.Text = stringInputText;
                    break;
                }
                catch (Exception ex) { string str = ex.ToString(); Utilities.GlobalObject.LOGGER.log(str); }
                Utilities.WaitUtil.checkIfTimeWasOut(stopWatch);
            }
        }

        public static void TypeTextBox(TextBox TB, string stringInputText)
        {
            //Start count time
            Stopwatch stopWatch = new Stopwatch();
            stopWatch.Start();
            while (true)
            {
                System.Threading.Thread.Sleep(Utilities.GlobalConfig.INT_WAIT_INTERVAL_MILISECOND);
                try
                {
                    TB.Text = stringInputText;
                    break;
                }
                catch { }
                Utilities.WaitUtil.checkIfTimeWasOut(stopWatch);
            }
        }

        //Checkbox
        public static void CheckCheckBox(string stringAutomationId, Boolean booleanCheckbox)
        {
            //Start count time
            Stopwatch stopWatch = new Stopwatch();
            stopWatch.Start();
            while (true)
            {
                Utilities.GlobalObject.SILVERLIGHTAPP.RefreshVisualTrees();
                CheckBox cb = Utilities.GlobalObject.SILVERLIGHTAPP.FindName<CheckBox>(stringAutomationId);
                System.Threading.Thread.Sleep(Utilities.GlobalConfig.INT_WAIT_INTERVAL_MILISECOND);
                try
                {
                    cb.IsChecked = booleanCheckbox;
                    break;
                }
                catch { }
                Utilities.WaitUtil.checkIfTimeWasOut(stopWatch);
            }
        }

        //ComboBox
        public static void SelectComboBox(ComboBox cmbBox, string strItem)
        {
            //Start count time
            Stopwatch stopWatch = new Stopwatch();
            stopWatch.Start();
            while (true)
            {
                cmbBox.User.Click();
                IList<TextBlock> IListItems = Utilities.GlobalObject.SILVERLIGHTAPP.Find.AllByType<TextBlock>();
                for (int i = 0; i < IListItems.Count; i++)
                {
                    if (IListItems[i].Text.Equals(strItem))
                    {
                        try
                        {
                            IListItems[i].User.Click();
                        }
                        catch { }
                    }
                }
                Utilities.WaitUtil.checkIfTimeWasOut(stopWatch);
            }
        }
    }
}
